package com.lwb.ztx.news.widget;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.NewsBean;
import com.lwb.ztx.bean.NewsDetailBean;
import com.lwb.ztx.database.GreenDaoManager;
import com.lwb.ztx.database.greendao.dao.CollectDao;
import com.lwb.ztx.database.greendao.db.Collect;
import com.lwb.ztx.news.presenter.NewsDetailPresenter;
import com.lwb.ztx.news.presenter.NewsDetailPresenterImpl;
import com.lwb.ztx.news.view.NewsDetailView;
import com.lwb.ztx.utils.LogUtils;
import com.lwb.ztx.utils.ToolsUtil;

import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class NewsDetailActivity extends SwipeBackActivity implements NewsDetailView {
    private NewsDetailPresenter presenter;
    private NewsBean newsBean;
    private SwipeBackLayout mSwipeBackLayout;
    @InjectView(R.id.news_detail_title)
    TextView titleTextView;
    @InjectView(R.id.news_detail_data)
    TextView dataTextView;
    @InjectView(R.id.news_detail_text)
    WebView textTextView;
    @InjectView(R.id.back)
    View backView;
    @InjectView(R.id.collect)
    CheckBox collectBtn;

    CollectDao dao;
    Collect collect;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIntent();
        setContentView(R.layout.activity_news_detail);
        ButterKnife.inject(this);
        presenter = new NewsDetailPresenterImpl(this);
        if ((null != newsBean) && newsBean.getLinkUrl().startsWith("http")) {
            presenter.getNewsDetail(newsBean.getLinkUrl());
        }
        collectBtn.setVisibility(View.VISIBLE);
        collectBtn.setChecked(collect != null);
    }

    private void initIntent() {
        newsBean = (NewsBean) getIntent().getSerializableExtra("news");
        LogUtils.i(newsBean.toString());
        dao =GreenDaoManager.getDaoSession(this).getCollectDao();
        collect = dao.load(newsBean.getDocumentId());
    }

    @Override
    public void setNewsDetail(NewsDetailBean bean) {
        titleTextView.setText(bean.getTitle());
        dataTextView.setText(newsBean.getFormatTime() + "  " + newsBean.getSource());
        WebSettings ws = textTextView.getSettings();
        textTextView.loadData(bean.getText(), "text/html; charset=UTF-8", null);
    }

    @OnClick({R.id.back, R.id.collect})
    public void onHeaderClick(View v) {
        switch (v.getId())  {
            case R.id.back:
                this.finish();
                break;
            case R.id.collect:
                if (collectBtn.isChecked()) {
                    if(collect == null) {
                        collect = new Collect();
                        collect.setCollectDocId(newsBean.getDocumentId());
                        collect.setCollectImageUrl(newsBean.getThumbnail());
                        collect.setCollectTime(new Date());
                        collect.setCollectTitle(newsBean.getTitle());
                        collect.setCollectType(newsBean.getType());
                        collect.setCollectUpdateTime(newsBean.getFormatTime());
                        collect.setCollectLinkUrl(newsBean.getLinkUrl());
                        collect.setCollectSource(newsBean.getSource());
                        dao.insert(collect);
                        Toast.makeText(this,"收藏成功！",Toast.LENGTH_SHORT).show();
                    }
                } else {
                    dao.delete(collect);
                    collect = null;
                    Toast.makeText(this,"取消收藏！",Toast.LENGTH_SHORT).show();
                }
                LogUtils.i(dao.loadAll().toString());
                break;
        }
    }
}
